package controller;

import domain.Provider;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.ProviderService;
import utils.PageBean;
import utils.Transform;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/provider")
public class ProviderController {

    @Resource
    private ProviderService providerService;

    /**
     * 查询所有供应商
     * @param request
     * @return
     */
    @RequestMapping("/queryAll.action")
    public String queryAll(HttpServletRequest request){
        String proCode = request.getParameter("queryProCode");
        String proName = request.getParameter("queryProName");
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex == null){
            pageIndex = "1";
        }
        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(Integer.parseInt(pageIndex));
        List<Provider> providerList = providerService.queryProviderByPage(proCode,proName,pageBean);
        int totalCount = providerService.totalCount();
        pageBean.setTotal(totalCount);
        request.setAttribute("providerList",providerList);
        request.setAttribute("totalPageCount",pageBean.getTotalPage());
        request.setAttribute("totalCount",totalCount);
        request.setAttribute("currentPageNo",pageIndex);
        return "forward:../jsp/providerlist.jsp";
    }

    @RequestMapping("/addProvider.action")
    public String addProvider(HttpServletRequest request){
        Map<String,String[]> params = request.getParameterMap();
        Provider provider = new Provider();
        boolean flag = false;
        //注册BeanUtils类型转化器
        Transform.beanUtilsConvert();

        try {
            BeanUtils.populate(provider,params);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        flag = providerService.addProvider(provider);
        if(flag){ //添加成功
            request.setAttribute("message","添加成功");
        }else{
            //添加失败
            request.setAttribute("message","添加失败");
        }
        return  "forward:../jsp/provideradd.jsp";
    }

    /**
     * 查看指定id的供应商
     * @param request
     * @return
     */
    @RequestMapping("/viewProvider.action")
    public String viewProvider(HttpServletRequest request){
        String id = request.getParameter("proid");
        String method  = request.getParameter("method");
        Provider provider = providerService.queryProviderById(Integer.parseInt(id));
        request.setAttribute("provider",provider);
        String path="";
        if(method.equals("view")){
            path = "forward:../jsp/providerview.jsp";
        }else if(method.equals("modify")){
            path = "forward:../jsp/providermodify.jsp";
        }else{
            path = "/provider/queryAll.action";
        }
        return path;
    }

    /**
     * 修改供应商
     * @param request
     * @return
     */
    @RequestMapping("/modifyProvider.action")
    public String modifyProvider(HttpServletRequest request){
        Provider provider = new Provider();
        boolean flag = false;
        Map<String,String[]> params = request.getParameterMap();
        //注册BeanUtils格式转化器
        Transform.beanUtilsConvert();
        //封装provider对象
        try {
            BeanUtils.populate(provider,params);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(provider.toString());
        //调用业务层
        flag = providerService.modifyProvider(provider);
        if(flag){
            request.setAttribute("message","修改成功");
        }else{
            request.setAttribute("message","修改失败");
        }
        request.setAttribute("provider",provider);
        return "forward:../jsp/providermodify.jsp";
    }

    /**
     * 删除供应商
     * @param request
     * @return
     */
    @RequestMapping("/deleteProvider.action")
    @ResponseBody
    public Object deleteProvider(HttpServletRequest request){
        String id = request.getParameter("proid");
        HashMap<String,Object> result = new HashMap<>();
        int flag = providerService.deleteProvider(Integer.parseInt(id));
        if(flag == 0){
            result.put("delResult","notexist");
        }else if(flag == 1699 ){
            result.put("delResult","false");
        }else if(flag == 1698){
            result.put("delResult","true");
        }else{
            result.put("delResult",flag);
        }
        return result;
    }

    /**
     * ajax获取所有供应商
     * @return
     */
    @RequestMapping("/queryAllProvider.action")
    @ResponseBody
    public Object queryAllProvider(){
        return providerService.queryAll();
    }
}
